﻿
using Microsoft.EntityFrameworkCore;
using FreeRedis;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;
using Newtonsoft.Json.Linq;
using CQIE.LabAssist.Models;
using System;
using CQIE.LabAssist.PublicHelp;
using System.Drawing;
using System.Data;
using brand.cyt369.com.dal.DbContexts;
namespace CQIE.LabAssist.DataAccesss
{

    public class CheckService : BaseService<CheckService>
    {


        //private readonly _DbContext _Db;
        //public CheckService(JwtHelp jwtHelper, _DbContext db)
        //{

        //    _Db = db;
        //}

        /// <summary>
        /// 新增检查表
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public bool addCheck(Check check)
        {
            using (var ctx = Ctx)
            {
                Check newcheck = new Check
                {
                    DateTime = DateTime.Today.Date.AddHours(0),
                    Anti_theft_facilities = check.Anti_theft_facilities,
                    Anti_theft_facilities_state = check.Anti_theft_facilities_state,
                    Doors_windows = check.Doors_windows,
                    Doors_windows_state = check.Doors_windows_state,
                    Electrical_wiring = check.Electrical_wiring,
                    Electrical_wiring_state = check.Electrical_wiring_state,
                    Firefighting_facilities = check.Firefighting_facilities,
                    Firefighting_facilities_state = check.Firefighting_facilities_state,
                    Firefighting_facilities_validity = check.Firefighting_facilities_validity,
                    Firefighting_facilities_validity_state = check.Firefighting_facilities_validity_state,
                    Instruments_equipment = check.Instruments_equipment,
                    Instruments_equipment_state = check.Instruments_equipment_state,
                    Warning_sign = check.Warning_sign,
                    Warning_sign_state = check.Warning_sign_state,
                    IEOther_safety_hazards = check.IEOther_safety_hazards,
                    IEOther_safety_hazards_state = check.IEOther_safety_hazards_state,
                    Item_hygiene = check.Item_hygiene,
                    Item_hygiene_state = check.Item_hygiene_state,
                    Other_safety_hazards = check.Other_safety_hazards,
                    Other_safety_hazards_state = check.Other_safety_hazards_state,
                    laboratoryId = check.laboratoryId,
                    semesterId = check.semesterId
                };
                if (check.Anti_theft_facilities && check.Doors_windows &&
                   check.Electrical_wiring && check.Electrical_wiring &&
                   check.Firefighting_facilities && check.Firefighting_facilities_validity &&
                   check.Instruments_equipment && check.Warning_sign && check.IEOther_safety_hazards &&
                   check.Item_hygiene && check.Other_safety_hazards)
                {
                    newcheck.State = true;
                }
                else
                {
                    newcheck.State = false;
                }
                newcheck.semester = Ctx.Semesters.Where(w => w.Id == check.semesterId).FirstOrDefault();
                newcheck.laboratory = Ctx.Laboratories.Where(W => W.Id == check.laboratoryId).FirstOrDefault();


                Ctx.Checks.Add(newcheck);
                return Ctx.SaveChanges() > 0;
            }

        }

        /// <summary>
        /// 获取所有检查记录
        /// </summary>
        /// <returns></returns>
        public JsonResult getAllCheck()
        {
            var checks = Ctx.Checks.Include(w => w.laboratory).ThenInclude(q => q.Admin).Include(w => w.semester)
                .Select(w => new
                {
                    Id=w.Id,
                    DateTime=w.DateTime,
                   // DateTime = w.DateTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    SemesterName =w.semester.Name,
                    AdminName = w.laboratory.Admin.Name,
                    LabName = w.laboratory.Name,
                    LabNumber = w.laboratory.Nubmer,
                    buildingId=w.laboratory.floor.buildingId,
                    laboratoryId = w.laboratoryId,
                    semesterId = w.semesterId,
                    State = w.State,
                    Doors_windows = w.Doors_windows,
                    Doors_windows_state = w.Doors_windows_state,
                    Electrical_wiring = w.Electrical_wiring,
                    Electrical_wiring_state = w.Electrical_wiring_state,
                    Firefighting_facilities = w.Firefighting_facilities,
                    Firefighting_facilities_state = w.Firefighting_facilities_state,
                    Firefighting_facilities_validity = w.Firefighting_facilities_validity,
                    Firefighting_facilities_validity_state = w.Firefighting_facilities_validity_state,
                    Instruments_equipment = w.Instruments_equipment,
                    Instruments_equipment_state = w.Instruments_equipment_state,
                    Anti_theft_facilities = w.Anti_theft_facilities,
                    Anti_theft_facilities_state = w.Anti_theft_facilities_state,
                    Warning_sign = w.Warning_sign,
                    Warning_sign_state = w.Warning_sign_state,
                    IEOther_safety_hazards = w.IEOther_safety_hazards,
                    IEOther_safety_hazards_state = w.IEOther_safety_hazards_state,
                    Item_hygiene = w.Item_hygiene,
                    Item_hygiene_state = w.Item_hygiene_state,
                    Other_safety_hazards = w.Other_safety_hazards,
                    Other_safety_hazards_state = w.Other_safety_hazards_state,
                    
                })
                .ToList();

            return new JsonResult(checks);
        }

        /// <summary>
        /// 获取记录通过实验员id
        /// </summary>
        /// <returns></returns>
        public JsonResult getCheckByUserId(int uid)
        {
            var checks = Ctx.Checks.Include(w => w.laboratory).ThenInclude(q => q.Admin).Include(w => w.semester).Where(w=>w.laboratory.UserId==uid)
                .Select(w => new
                {
                    Id = w.Id,
                    DateTime = w.DateTime,
                    // DateTime = w.DateTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    SemesterName = w.semester.Name,
                    AdminName = w.laboratory.Admin.Name,
                    LabName = w.laboratory.Name,
                    LabNumber = w.laboratory.Nubmer,
                    laboratoryId = w.laboratoryId,
                    State = w.State,
                    Doors_windows = w.Doors_windows,
                    Doors_windows_state = w.Doors_windows_state,
                    Electrical_wiring = w.Electrical_wiring,
                    Electrical_wiring_state = w.Electrical_wiring_state,
                    Firefighting_facilities = w.Firefighting_facilities,
                    Firefighting_facilities_state = w.Firefighting_facilities_state,
                    Firefighting_facilities_validity = w.Firefighting_facilities_validity,
                    Firefighting_facilities_validity_state = w.Firefighting_facilities_validity_state,
                    Instruments_equipment = w.Instruments_equipment,
                    Instruments_equipment_state = w.Instruments_equipment_state,
                    Anti_theft_facilities = w.Anti_theft_facilities,
                    Anti_theft_facilities_state = w.Anti_theft_facilities_state,
                    Warning_sign = w.Warning_sign,
                    Warning_sign_state = w.Warning_sign_state,
                    IEOther_safety_hazards = w.IEOther_safety_hazards,
                    IEOther_safety_hazards_state = w.IEOther_safety_hazards_state,
                    Item_hygiene = w.Item_hygiene,
                    Item_hygiene_state = w.Item_hygiene_state,
                    Other_safety_hazards = w.Other_safety_hazards,
                    Other_safety_hazards_state = w.Other_safety_hazards_state,

                })
                .ToList();

            return new JsonResult(checks);
        }

        /// <summary>
        /// 获取记录通过实验室id
        /// </summary>
        /// <returns></returns>
        public JsonResult getCheckByLabId(int lid)
        {
            var checks = Ctx.Checks.Where(w=>w.laboratoryId==lid).Include(w => w.laboratory).ThenInclude(q => q.Admin).Include(w => w.semester)
                .Select(w => new
                {
                    Id = w.Id,
                    DateTime = w.DateTime,
                    // DateTime = w.DateTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    SemesterName = w.semester.Name,
                    AdminName = w.laboratory.Admin.Name,
                    LabName = w.laboratory.Name,
                    LabNumber = w.laboratory.Nubmer,
                    laboratoryId = w.laboratoryId,
                    State = w.State,
                    Doors_windows = w.Doors_windows,
                    Doors_windows_state = w.Doors_windows_state,
                    Electrical_wiring = w.Electrical_wiring,
                    Electrical_wiring_state = w.Electrical_wiring_state,
                    Firefighting_facilities = w.Firefighting_facilities,
                    Firefighting_facilities_state = w.Firefighting_facilities_state,
                    Firefighting_facilities_validity = w.Firefighting_facilities_validity,
                    Firefighting_facilities_validity_state = w.Firefighting_facilities_validity_state,
                    Instruments_equipment = w.Instruments_equipment,
                    Instruments_equipment_state = w.Instruments_equipment_state,
                    Anti_theft_facilities = w.Anti_theft_facilities,
                    Anti_theft_facilities_state = w.Anti_theft_facilities_state,
                    Warning_sign = w.Warning_sign,
                    Warning_sign_state = w.Warning_sign_state,
                    IEOther_safety_hazards = w.IEOther_safety_hazards,
                    IEOther_safety_hazards_state = w.IEOther_safety_hazards_state,
                    Item_hygiene = w.Item_hygiene,
                    Item_hygiene_state = w.Item_hygiene_state,
                    Other_safety_hazards = w.Other_safety_hazards,
                    Other_safety_hazards_state = w.Other_safety_hazards_state,

                })
                .ToList();

            return new JsonResult(checks);
        }
        

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="checkId"></param>
        /// <returns></returns>
        public bool deteleCheck(int checkId)
        {
            using (var ctx = Ctx)
            {
                var de = ctx.Checks.Where(w => w.Id == checkId).FirstOrDefault();
                ctx.Checks.Remove(de);
                return ctx.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="check"></param>
        public bool updateCheck( Check check)
        {
            using (var ctx = Ctx)
            {
                // 从数据库中检索现有的检查记录
                var existingCheck = ctx.Checks.Where(w => w.Id == check.Id).FirstOrDefault();

                // 检查实体是否存在
                if (existingCheck == null)
                {
                    // 如果实体不存在，返回 false
                    return false;
                }

                // 直接更新现有实体的属性
                existingCheck.DateTime = check.DateTime;
                existingCheck.Anti_theft_facilities = check.Anti_theft_facilities;
                existingCheck.Anti_theft_facilities_state = check.Anti_theft_facilities_state;
                existingCheck.Doors_windows = check.Doors_windows;
                existingCheck.Doors_windows_state = check.Doors_windows_state;
                existingCheck.Electrical_wiring = check.Electrical_wiring;
                existingCheck.Electrical_wiring_state = check.Electrical_wiring_state;
                existingCheck.Firefighting_facilities = check.Firefighting_facilities;
                existingCheck.Firefighting_facilities_state = check.Firefighting_facilities_state;
                existingCheck.Firefighting_facilities_validity = check.Firefighting_facilities_validity;
                existingCheck.Firefighting_facilities_validity_state = check.Firefighting_facilities_validity_state;
                existingCheck.Instruments_equipment = check.Instruments_equipment;
                existingCheck.Instruments_equipment_state = check.Instruments_equipment_state;
                existingCheck.Warning_sign = check.Warning_sign;
                existingCheck.Warning_sign_state = check.Warning_sign_state;
                existingCheck.IEOther_safety_hazards = check.IEOther_safety_hazards;
                existingCheck.IEOther_safety_hazards_state = check.IEOther_safety_hazards_state;
                existingCheck.Item_hygiene = check.Item_hygiene;
                existingCheck.Item_hygiene_state = check.Item_hygiene_state;
                existingCheck.Other_safety_hazards = check.Other_safety_hazards;
                existingCheck.Other_safety_hazards_state = check.Other_safety_hazards_state;
                existingCheck.laboratoryId = check.laboratoryId;
                existingCheck.semesterId = check.semesterId;

                // 更新状态属性

                if (check.Anti_theft_facilities && check.Doors_windows &&
                   check.Electrical_wiring && check.Electrical_wiring &&
                   check.Firefighting_facilities && check.Firefighting_facilities_validity &&
                   check.Instruments_equipment && check.Warning_sign && check.IEOther_safety_hazards &&
                   check.Item_hygiene && check.Other_safety_hazards)
                {
                    existingCheck.State = true;
                }
                else
                {
                    existingCheck.State = false;
                }

                // 更新关联的实验室和学期实体
                //existingCheck.semester = Ctx.Semesters.Where(w => w.Id == check.semesterId).FirstOrDefault();
                //existingCheck.laboratory = Ctx.Laboratories.Where(w => w.Id == check.laboratoryId).FirstOrDefault();

                // 更新数据库
                // Ctx.Checks.Update(existingCheck);
                return ctx.SaveChanges() > 0;
            }
        }
    }


}
